package summary;

import java.util.Arrays;

/**
 * @Author: 海琳琦
 * @Date: 2022/8/28 17:28
 */
public class Title56 {

    public static int[][] merge(int[][] intervals) {
        //按照区间左端点进行排序
        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
        int[][] arr = new int[intervals.length][2];
        int idx = -1;
        for (int[] temp : intervals) {
            //左端点大于右端点时，区间不重叠
            System.out.println(idx);
            if(idx == -1 || temp[0] > arr[idx][1]){
                arr[++idx] = temp;
            }else{
                arr[idx] = new int[]{arr[idx][0], Math.max(arr[idx][1], temp[1])};
            }
        }
        return Arrays.copyOf(arr, idx + 1);
    }

    public static void main(String[] args) {
        int[][] arr = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
        merge(arr);
    }
}
